
class sightlineAnalysis{
  constructor(viewer) {
    this.viewer = viewer
    this.sightline=undefined
    this.sightLineHandler=undefined
    this.pointHandler = undefined;
    this.pointPosition=undefined
    this.longitude=undefined
    this.latitude=undefined
    this.height=undefined
    this.targetPoint=undefined
    //this.clickFlag = 0
    this.handler=undefined
    this.sightObservationPlace=''
  }

  remove(){
    if (this.sightLineHandler) {
      this.sightLineHandler.deactivate();
    }
    if (this.pointHandler) {
      this.pointHandler.deactivate();
    }
    this.handler && (!this.handler.isDestroyed()) && this.handler.destroy();
    this.viewer.entities.removeAll();
    if (this.sightline) {
      this.sightline.destroy();
      this.sightline = undefined;
    }
  }

  analysis(visibleColor,hiddenColor){
    const scene = this.viewer.scene;
    for (let layer of scene.layers.layerQueue) {
      layer.removeAllObjsColor();
    }
    if (!this.sightline) {
      this.sightline = new Cesium.Sightline(scene);
      this.sightline.build();
    }
    //this.clickFlag += 1
    this.sightline.removeAllTargetPoint()
    this.sightline.visibleColor = Cesium.Color.fromCssColorString(visibleColor)
    this.sightline.hiddenColor = Cesium.Color.fromCssColorString(hiddenColor)
    this.viewer.entities.removeAll()
    if (this.handler && !this.handler.isDestroyed()) {
      this.handler.destroy();
    }
    this.handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
    this.sightLineHandler = new Cesium.DrawHandler(this.viewer, Cesium.DrawMode.Line)
    this.sightLineHandler.activeEvt.addEventListener((isActive)=> {
      if (isActive == true) {
        this.viewer.enableCursorStyle = false
        this.viewer._element.style.cursor = ''
        if(document.getElementById('scene').classList.contains('drawCur')){
          document.getElementById('scene').classList.remove('drawCur')
        }
        document.getElementById('scene').classList.add('drawCur')
      }
      else {
        this.viewer.enableCursorStyle = true;
        document.getElementById('scene').classList.remove('drawCur')
      }
    })
    this.sightLineHandler.movingEvt.addEventListener((windowPosition)=>{
      this.sightLineHandler.polyline && (this.sightLineHandler.polyline.show = false)
    })
    this.sightLineHandler.drawEvt.addEventListener((result)=> {
      var line = result.object;
      var endPoint = line._positions[line._positions.length - 1];
      var ecartographic = Cesium.Cartographic.fromCartesian(endPoint);
      var elongitude = Cesium.Math.toDegrees(ecartographic.longitude);
      var elatitude = Cesium.Math.toDegrees(ecartographic.latitude);
      var eheight = ecartographic.height;
      this.targetPoint = [elongitude, elatitude, eheight];
      this.sightline.addTargetPoint({
        position: this.targetPoint,
        name: "point" + new Date()
      });
      this.handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
    });
    this.sightLineHandler.activate()
    this.pointHandler = new Cesium.PointHandler(this.viewer);
    //鼠标点击第一下，调用drawEvt；再点击，调用handler.setInputAction
    this.pointHandler.drawEvt.addEventListener((result)=>{
      let point = result.object;
      point.show = false;
      this.pointPosition = point;
      let position = point.position;
      let cartographic = Cesium.Cartographic.fromCartesian(position);
      this.longitude = Cesium.Math.toDegrees(cartographic.longitude);
      this.latitude = Cesium.Math.toDegrees(cartographic.latitude);
      this.height = cartographic.height;
      this.viewer.entities.add({
        position: position,
        point: {
          pixelSize: 10,
          color: Cesium.Color.WHITE
        }
      });

      this.sightObservationPlace = this.longitude.toFixed(4) + ', ' + this.latitude.toFixed(4) + ', ' + this.height.toFixed(2);
      document.getElementById('formSightlineobserverInformation').value=this.sightObservationPlace
      this.sightline.viewPosition = [this.longitude, this.latitude, this.height]
      this.handler.setInputAction((evt)=> {
        this.sightLineHandler.polyline && (this.sightLineHandler.polyline.show = false)
        let pick = this.viewer.scene.pickPosition(evt.position)
        let ecartographic = Cesium.Cartographic.fromCartesian(pick)
        let elongitude = Cesium.Math.toDegrees(ecartographic.longitude)
        let elatitude = Cesium.Math.toDegrees(ecartographic.latitude)
        let eheight = ecartographic.height
        this.sightline.addTargetPoint({
          position: [elongitude, elatitude, eheight],
          name: "point" + new Date()
        })
      }, Cesium.ScreenSpaceEventType.LEFT_CLICK)
    })
    this.pointHandler.activate()
  }

  changeVisibleColor(value){
    let color = Cesium.Color.fromCssColorString(value)
    if(this.sightline){
      this.sightline.visibleColor = color
    }
  }

  changeHiddenColor(value){
    let color = Cesium.Color.fromCssColorString(value)
    if(this.sightline){
      this.sightline.hiddenColor = color
    }
  }

  highlightBarrier(value){
    if (this.sightline) {
      if (this.sightLineHandler) {
        this.sightLineHandler.deactivate();
      }
      if (this.pointHandler) {
        this.pointHandler.deactivate();
      }
      this.handler && (!this.handler.isDestroyed()) && this.handler.destroy();
      let sightlineHighlightBarrierColor = Cesium.Color.fromCssColorString(value)
      for (let index in this.sightline.getObjectIds()) {
        let layer = viewer.scene.layers.findByIndex(index - 3); // 底层索引从3开始
        layer.setObjsColor(this.sightline.getObjectIds()[index], sightlineHighlightBarrierColor);
      }
    }
  }
}

export {sightlineAnalysis}

